package com.base.myproject.client.HR.Salary;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.print.Printer;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.user.client.ui.Widget;

public class SalaryX extends BaseJDBCPanel implements Printer{
	BaseStore mstore = new BaseStore();
	BaseStore mstore2 = new BaseStore();
	String bcode;
	String icode;
	Button show = new Button("显示详细");
	TabPanel tabpanel = new TabPanel();
	public SalaryX()
	{
		this(null);
	}
	
	public SalaryX(String icode)
	{
		
		this.icode =icode;// "00025";
		init();
	}
	public void init()
	{
		init_store();
		this.add(initWeigetx());
		registerMainStore(Salarystore);
		registerStore(Salarystore);
		registerStore(hr_salarystore);
		show.addListener(Events.OnClick, getListener());
		//registerStore(dstore);
		//inistore();
	}
	
	public void complete() {
		super.complete();
		getToolbarll().add(show);
	}

	private Widget initWeigetx() {
		VerticalPanel v = new VerticalPanel();
		HorizontalPanel h = new HorizontalPanel();
		h.setVerticalAlign(VerticalAlignment.TOP);
		h.setHorizontalAlign(HorizontalAlignment.LEFT);

		h.add(createSalaryGrid());
		h.add(createGrid());
		h.setSpacing(5);
		v.add(h);
		//v.add(show);
		
		tabpanel.setVisible(true);
		tabpanel.setWidth(990);
		tabpanel.setHeight(300);
		TabItem t1 = new TabItem();
		t1.add(createGrid1());
		t1.setText("发薪人数");
		t1.setScrollMode(Scroll.AUTO);
		
		TabItem t2 = new TabItem();
		t2.add(createGrid2());
		t2.setText("上月离职员工");
		t2.setScrollMode(Scroll.AUTO);
		
		TabItem t3 = new TabItem();
		t3.add(createGrid2());
		//t3.addText("dddddd3");
		t3.setText("本月入职员工");
		t3.setScrollMode(Scroll.AUTO);
		
		TabItem t4 = new TabItem();
		t4.add(createGrid2());
		//t4.addText("dddddd4");
		t4.setText("上月停发员工");
		t4.setScrollMode(Scroll.AUTO);
		
		TabItem t5 = new TabItem();
		t5.add(createGrid2());
		//t5.addText("dddddd5");
		t5.setText("本月停发员工");
		t5.setScrollMode(Scroll.AUTO);
		
		TabItem t6 = new TabItem();
		t6.add(createGrid2());
		//t6.addText("dddddd6");
		t6.setText("本月入职但晚于截止日期");
		t6.setScrollMode(Scroll.AUTO);
		
		TabItem t7 = new TabItem();
		t7.add(createGrid2());
		//t7.addText("dddddd7");
		t7.setText("上月入职但晚于截止日期");
		t7.setScrollMode(Scroll.AUTO);
		
		
		tabpanel.add(t1);
		tabpanel.add(t2);
		tabpanel.add(t3);
		tabpanel.add(t4);
		tabpanel.add(t5);
		tabpanel.add(t6);
		tabpanel.add(t7);
		
		v.add(tabpanel);
		return v;
	}
	
	BaseStore<BusiObject> Salarystore = null;

	BaseStore<BusiObject> getSalaryStore() {
		if (Salarystore == null) {
			createSalaryStore();
		}
		return Salarystore;
	}
	BaseStore<BusiObject> createSalaryStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("amount1");
		type.getField("amount1").setType(Double.class);
		type.addField("amount2");
		type.getField("amount2").setType(Double.class);
		type.addField("amount3");
		type.getField("amount3").setType(Double.class);
		type.addField("amount4");
		type.getField("amount4").setType(Double.class);
		type.addField("amount5");
		type.getField("amount5").setType(Double.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("null");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select bcode,sum(isnull(amount1,0)) as amount1,sum(isnull(amount2,0)) as amount2,sum(isnull(amount3,0)) as amount3,sum(isnull(amount4,0)) as amount4,sum(isnull(amount5,0)) as amount5  from (select bcode, case when salarytype='1' then sum(amount) end as amount1,  case when salarytype='2' then sum(amount) end as amount2, case when salarytype='3' then sum(amount) end as amount3, case when salarytype='4' then sum(amount) end as amount4,case when salarytype='5' then sum(amount) end as amount5 from hr_salary where "+(icode==null?" 1=1 ":(" upcode='"+icode+"' ") )+"group by bcode,salarytype )as a group by bcode");

		info.setNeedAllRow("false");
		info.setForcenolimit("true");
		Salarystore = new BaseStore<BusiObject>(type, info);
		return Salarystore;
	}

	BaseGrid Salarygrid = null;

	BaseGrid createSalaryGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("bcode", "人员", 80);
		
		BaseComboBoxForm<BaseModelData> bsb2 = new BaseComboBoxForm<BaseModelData>("bcode","bname","bcode","",true);	
		C1.setRenderer(bsb2);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("amount1", "工资", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("amount2", "津贴", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("amount3", "社保", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("amount4", "个税", 80);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("amount5", "花红", 80);
		columns.add(C6);

		ColumnModel cm = new ColumnModel(columns);
		Salarygrid = new BaseGrid(getSalaryStore(), cm,false,false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		Salarygrid.setSelectionModel(cbs);
		//Salarygrid.setTrackMouseOver(true);
		Salarygrid.addListener(Events.RowDoubleClick,getListener());
		Salarygrid.setHeight(256);
		Salarygrid.setWidth(524);
		return Salarygrid;
	}
	BaseStore<BusiObject> hr_salarystore = null;

	BaseStore<BusiObject> getStore() {
		if (hr_salarystore == null) {
			createhr_salaryStore();
		}
		return hr_salarystore;
	}

	BaseStore<BusiObject> createhr_salaryStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("allowanceno_");
		type.getField("allowanceno_").setType(String.class);
		type.addField("amount");
		type.getField("amount").setType(Double.class);
		type.addField("status");
		type.getField("status").setType(Integer.class);
		type.addField("createdate");
		type.getField("createdate").setType(Date.class);
		type.addField("vprepare");
		type.getField("vprepare").setType(String.class);
		type.addField("salarydate");
		type.getField("salarydate").setType(Date.class);
		type.addField("upcode");
		type.getField("upcode").setType(String.class);
		type.addField("bname");
		type.getField("bname").setType(String.class);
		type.addField("desc_cn");
		type.getField("desc_cn").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_salary");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_salary where bcode='{bcode}' and  "+(icode==null?" 1=1 ":(" upcode='"+icode+"' ") )+"");
		info.setParent(getSalaryStore());// 关联主表
		info.setLinkparentcolumns(new String[] { "bcode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "bcode" });// 关联主表字段
		hr_salarystore = new BaseStore<BusiObject>(type, info);
		return hr_salarystore;
	}
	
	BaseGridPanel salarydetailgrid = null;

	BaseGridPanel createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		// ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
		// columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("bcode", "人员", 80);
		BaseComboBoxForm<BaseModelData> bsb2 = new BaseComboBoxForm<BaseModelData>("bcode","bname","bcode","",true);
		
		C2.setRenderer(bsb2);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("allowanceno_", "薪金类别", 80);
		BaseComboBoxForm<BaseModelData> bsb3 = new BaseComboBoxForm<BaseModelData>("code","desc_cn","hr_salary_item","",true);
		
		C3.setRenderer(bsb3);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("amount", "金额", 80);
		NumberField C_num4 = new  NumberField(); 
		C4.setEditor(new CellEditor(C_num4));
		columns.add(C4);

		ColumnModel cm = new ColumnModel(columns)
		{
			 public String getColumnStyle(int colIndex) {
				 if(colIndex==4)
				 {
					
					    return " background-color: #FFFF99 ;";
				 }
				
				    return configs.get(colIndex).getStyle();

				  }
		};
		salarydetailgrid = new BaseGridPanel(createhr_salaryStore(), cm, false, false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		salarydetailgrid.getBg().setSelectionModel(cbs);
		salarydetailgrid.getBg().setHeight(234);
		salarydetailgrid.getBg().setWidth(313);	
		return salarydetailgrid;
	}
	
	/************************/
	
	// 创建一个grid

	BaseGrid<BusiObject> createGrid1() {
		BaseGrid<BusiObject> grid = null;
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C2 = new ColumnConfig("m1", "上月发薪人数", 95);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("m2", "本月发薪人数", 95);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("m3", "上月离职人数", 95);
		columns.add(C4);	
		ColumnConfig C5 = new ColumnConfig("m4", "本月入职人数", 95);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("m5", "上月停发人数", 95);
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("m6", "本月停发人数", 95);
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("m7", "本月入职但晚于截止日期的人数", 185);
		columns.add(C8);
		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(mstore, cm ,false,false);
		cbs.setSelectionMode(SelectionMode.SIMPLE);//控制是否可以多选每行后面的复选框
		grid.setSelectionModel(cbs);
		grid.setHeight(222);
		grid.setWidth(970);
		return grid;
		//Tools.setResize(grid,0.99,0.15);
	}	
	
	
	
	BaseGrid<BusiObject> createGrid2() {
		BaseGrid<BusiObject> grid = null;
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C2 = new ColumnConfig("bname", "姓名", 45);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("bcode", "员工编号", 65);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("depName", "部门", 65);
		columns.add(C4);		
		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(mstore2, cm ,false,false);
		cbs.setSelectionMode(SelectionMode.SIMPLE);//控制是否可以多选每行后面的复选框
		grid.setSelectionModel(cbs);
		grid.setHeight(222);
		grid.setWidth(970);
		return grid;
		//Tools.setResize(grid,0.99,0.15);
	}	
	
	/***************************/
	
	
	
	
	
	
	
	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
			
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("输入型变量")) {

							}
						}else if (be.getType() == Events.OnClick) {
							if (be.getSource().equals(show)) {
								tabpanel.setVisible(true);
								BusiObject bo = new BusiObject();
								bo.set("m1", "1");
								bo.set("m2", "2");
								bo.set("m3", "3");
								bo.set("m4", "4");
								bo.set("m5", "5");
								bo.set("m6", "6");
								bo.set("m7", "7");
								mstore.add(bo,false,false);
								mstore.reload();
							}
						} 
						else if(be.getType().equals(Events.RowDoubleClick))
						{
						}
						
						else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
	@Override
	public void Print() {
		// TODO Auto-generated method stub
		
	}
	
	void init_store(){
		BusiObject bo = new BusiObject();
		bo.set("m1", "1");
		bo.set("m2", "2");
		bo.set("m3", "3");
		bo.set("m4", "4");
		bo.set("m5", "5");
		bo.set("m6", "6");
		bo.set("m7", "7");
		mstore.add(bo,false,false);
	}

}
